package com.example.project.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.project.core.text.Convert;
import com.example.project.entity.Issue;
import com.example.project.entity.Project;
import com.example.project.mapper.ProjectMapper;
import com.example.project.service.ProjectService;
import com.example.project.utils.CacheUtils;
import com.example.project.utils.DateUtils;
import com.example.project.utils.StringUtils;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-15
 */
@Service
public class ProjectServiceImpl extends BaseServiceImpl implements ProjectService {

	@Autowired
	private ProjectMapper projectMapper;

	/**
	 * 查询项目
	 * 
	 * @param id 项目主键
	 * @return 项目
	 */
	@Override
	public Project selectProjectById(Long id) {
		return projectMapper.selectProjectById(id);
	}

	/**
	 * 查询项目列表
	 * 
	 * @param project 项目
	 * @return 项目
	 */
	@Override
	public List<Project> selectProjectList(Project project) {
		Page<Issue> page = new Page<>(1, Integer.MAX_VALUE);
		List<Project> lists = projectMapper.selectProjectList(page,project);
		return lists;
	}

	/**
	 * 新增项目
	 * 
	 * @param project 项目
	 * @return 结果
	 */
	@Override
	public int insertProject(Project project) {
		project.setCreateTime(DateUtils.curLocalDateTime());
		String identifier = project.getIdentifier();
		// 验证identifier格式：长度1-100字符，只包含小写字母、数字、破折号和下划线
		if (StringUtils.isEmpty(identifier) || identifier.length() < 1 || identifier.length() > 100
				|| !identifier.matches("^[a-z0-9\\-_]+$")) {
			return -2;
		}
		int checkIdentifier = projectMapper.checkIdentifier(identifier, 0L);
		if (checkIdentifier >= 1) {
			return -1;
		}
		project.setState(1);
		int insertProject = projectMapper.insertProject(project);
		CacheUtils.remove("projectList");
		return insertProject;
	}

	/**
	 * 修改项目
	 * 
	 * @param tProject 项目
	 * @return 结果
	 */
	@Override
	public int updateProject(Project tProject) {
		tProject.setUpdateTime(DateUtils.curLocalDateTime());
		CacheUtils.remove("projectList");
		return projectMapper.updateProject(tProject);
	}

	/**
	 * 批量删除项目
	 * 
	 * @param ids 需要删除的项目主键
	 * @return 结果
	 */
	@Override
	public int deleteProjectByIds(String ids) {
		CacheUtils.remove("projectList");
		return projectMapper.deleteProjectByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除项目信息
	 * 
	 * @param id 项目主键
	 * @return 结果
	 */
	@Override
	public int deleteProjectById(Long id) {
		CacheUtils.remove("projectList");
		return projectMapper.deleteProjectById(id);
	}

	/**
	 * 查询全部项目列表
	 * 
	 * @return 项目列表
	 */
	@Override
	public List<Project> findAllList(int state) {
		return projectMapper.selectProjectAll(state);
	}
	
	/**
	 * 用户状态修改
	 * 
	 * @param projects 用户信息
	 * @return 结果
	 */
	@Override
	public int changeStatus(Project projects) {
		CacheUtils.remove("projectList");
		return projectMapper.updateStatus(projects.getId(), projects.getState());
	}

	@Override
	public long getCountByState(int state) {
		// TODO Auto-generated method stub
		return	projectMapper.selectCountByState(state);
//		return projectsMapper.getCount();
	}
}
